import logging
import time

from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.chrome.webdriver import WebDriver
from ...constants.common_sequence import SWITCH_ENV

from ...page.common.common_page import CommonPage
from ...page.order_page.append_from_pre_order_page import AppendFromPreOrderPage
from ...page.order_page.order_page import OrderPage
from ...page.pay_page.pay_out_page import PayOutPage


class TablePage(object):

    def __init__(self, client):
        self.client = client
        self.driver: WebDriver = client.driver
        self.common = CommonPage(self.client)

    """
    Service Function
    """

    def time_wait(self, seconds):
        """
        元素等待
        """
        self.driver.implicitly_wait(seconds)
        return self

    def time_sleep(self, seconds):
        """
        Time模块睡眠
        Params:
        -seconds:秒
        """
        time.sleep(seconds)
        return self

    def clear_table_manual(self, name):
        """
        手动清除桌台
        Params:
        -name:桌台名称
        """
        self.driver.find_element(AppiumBy.XPATH, f'//android.widget.TextView[@resource-id="{SWITCH_ENV}:id/tv_name" and @text="{name}"]').click()
        self.add_meal_from_pre().clean_table()
        return self

    def enter_table_manual(self,name):
        """
        进入桌台
        Params:
        -name:桌台名称
        """
        self.driver.find_element(AppiumBy.XPATH, f'//android.widget.TextView[@resource-id="{SWITCH_ENV}:id/tv_name" and @text="{name}"]').click()
        return self

    """
    SubPage Function
    """

    def clear_table(self):
        """
        清除桌台(桌台加单后自动弹出的弹窗清除桌台)
        """
        self.driver.find_element(AppiumBy.XPATH, f'//android.widget.TextView[@resource-id="{SWITCH_ENV}:id/tv_sure"]').click()
        return self

    def add_meal_from_pre(self):
        """
        加载已点
        """
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_already_added').click()
        return AppendFromPreOrderPage(self.client)

    def switch_table(self):
        """
        换桌
        """
        self.common.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_changing_tables').click()

    def proforma_invoice(self):
        """
        打印预结单
        """
        self.common.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/ll_print_receipt').click()

    def forward_to_pay(self):
        """
         去支付
        """
        pay_elements_one=self.driver.find_elements(AppiumBy.ID, f'{SWITCH_ENV}:id/rl_cart_cashier')
        pay_elements_two=self.driver.find_elements(AppiumBy.ID,f'{SWITCH_ENV}:id/tv_table_checkout')
        if len(pay_elements_one):
            pay_elements_one[0].click()
            return PayOutPage(self.client)
        if len(pay_elements_two):
            pay_elements_two[0].click()
            return PayOutPage(self.client)
        return PayOutPage(self.client)



    """
    Button Function
    """

    def status_switch(self, name):
        """
        切换状态
        Params:
        -name:状态名称
        """
        try:
            self.driver.find_element(AppiumBy.XPATH,
                                     f'//android.widget.TextView[@resource-id="{SWITCH_ENV}:id/tvOption" and @text="{name.strip()}"] ').click()
        except Exception as e:
            logging.error("传递的名称存在问题！")
        return self

    def table_area(self, name):
        """
        桌台分区选择
        Params:
        -name:桌台名称
        """
        try:
            self.driver.find_element(AppiumBy.XPATH,
                                     f'//android.widget.TextView[@resource-id="{SWITCH_ENV}:id/tvOption" and @text="{name.strip()}"]').click()
        except Exception as e:
            logging.error("传递的名称存在问题！")
        return self

    def select_table(self, name, number):
        """
        选择桌台
        Params:
        -name:桌台名称
        -number:桌台人数
        """
        try:
            self.driver.find_element(AppiumBy.XPATH,
                                     f'//android.widget.TextView[@resource-id="{SWITCH_ENV}:id/tv_name" and @text="{name}"]').click()
        except Exception as e:
            logging.error("传递的桌台名称存在问题！")
        self.driver.implicitly_wait(1)
        for i in range(number):
            self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/btn_add').click()
        self.driver.find_element(AppiumBy.ID, f'{SWITCH_ENV}:id/tv_open_table').click()
        return OrderPage(self.client)
